package InformationRetriever.QueryMethods;

import Database.Document;
import Database.DocumentVector;

import java.util.Comparator;

public class JaccardCoefficientQuery extends QueryBase implements Comparator<Document>
{    
    public DocumentVector PerformQuery(String query)
    {
        return this.getSortedDocuments(query, this);
    }
    
    public double compareToQuery(Document o)
    {
        ///get (column) index of document o in transposed v2...
        int index = documents.indexOf(o);
        
        ///apply the formula...        
        double numerator = 0;
        for(int i = 0; i < transposedV2.getRowDimension(); i++)
            numerator += queryVectorInTransposedV2.get(0,i) * transposedV2.get(i,index);
        
        double sigmaOfSquares1 = 0;
        for(int i = 0; i < transposedV2.getRowDimension(); i++)
            sigmaOfSquares1 += queryVectorInTransposedV2.get(0,i) * queryVectorInTransposedV2.get(0,i);
        
        double sigmaOfSquares2 = 0;
        for(int i = 0; i < transposedV2.getRowDimension(); i++)
            sigmaOfSquares2 += transposedV2.get(i,index) * transposedV2.get(i,index);
        
        return numerator/(sigmaOfSquares1 + sigmaOfSquares2 - numerator);
    }

    public int compare(Document o1, Document o2)
    {
        return (int)(compareToQuery(o2)*1000 - compareToQuery(o1)*1000);
    }
    
    public boolean equals(Object obj)
    {
        return false;
    }
}